package com.e3roid.examples;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Background;
import com.e3roid.drawable.Layer;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.texture.AssetTexture;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.event.SceneUpdateListener;

public class HUDExample extends E3Activity implements SceneUpdateListener {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private Texture robotTexture;
	private Texture hudTexture;
	
	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.registerUpdateListener(1000, this);
		
		int centerX = (getWidth()  - hudTexture.getWidth())  / 2;
		int centerY = (getHeight() - hudTexture.getHeight()) / 2;
		
		Sprite hud = new Sprite(hudTexture, centerX, centerY);
		scene.addHUD(hud);
		
		centerX = (getWidth()  - robotTexture.getWidth())  / 2;
		centerY = (getHeight() - robotTexture.getHeight()) / 2;
		
		Background background = new Background(
				new TiledTexture("background.png", getWidth(), getHeight(), this));
		scene.getTopLayer().setBackground(background);
		
		Sprite robot = new Sprite(robotTexture, centerX, centerY);
		scene.getTopLayer().add(robot);
		
		return scene;
	}

	@Override
	public void onLoadResources() {
		robotTexture = new AssetTexture("robot.png", this);
		hudTexture  =  new AssetTexture("controller_base.png", this);
	}

	/*
	 * Update the scene to move the layer. HUD Sprite dosen't move
	 * while robot Sprite and Background sprite are moving.
	 */
	@Override
	public void onUpdateScene(final E3Scene scene, final long elapsedMsec) {
		Layer layer = scene.getTopLayer();
		int x = layer.getX();
		if (x > getWidth()) {
			x = -getWidth();
		} else {
			x += 10;
		}
		layer.moveX(x);
	}
}
